package com.xwd.dao.impl;

import com.xwd.dao.BookDao;
import com.xwd.pojo.Book;
import com.xwd.utils.ConnectionPool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BookDaoImpl
 * @Description: com.xwd.dao.impl
 * @Auther: xiwd
 * @Date: 2022/2/13 - 02 - 13 - 13:11
 * @version: 1.0
 */
public class BookDaoImpl implements BookDao {
    @Override
    public Long findTotalSize(String sql, Object ...args) {
        QueryRunner queryRunner=new QueryRunner();
        //获取Connection对象
        Connection connection = ConnectionPool.popConnection();
        Long queryRows=0L;
        //查询操作
        try {
            queryRows = queryRunner.query(connection, sql ,args,new ScalarHandler<Long>());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionPool.pushConnection(connection);
        }
        return queryRows;
    }

    //methods
    @Override
    public List<Book> queryList(String sql, Object... args) {
        QueryRunner runner=new QueryRunner();
        //获取Connection对象
        Connection connection = ConnectionPool.popConnection();
        //执行sql语句
        List<Book> books = null;
        try {
            books = runner.query(connection, sql, args, new BeanListHandler<Book>(Book.class));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionPool.pushConnection(connection);
        }
        return books;
    }

    @Override
    public Book queryForObject(String sql, Object... args) {
        QueryRunner queryRunner=new QueryRunner();
        Book book = null;
        //获取数据库连接Connection对象
        Connection connection = ConnectionPool.popConnection();
        //beanhander
        BeanHandler<Book> beanHandler = new BeanHandler<>(Book.class);
        //执行查询操作
        try {
           book = queryRunner.query(connection, sql, beanHandler , args);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionPool.pushConnection(connection);
        }
        return book;
    }

    @Override
    public List<Book> queryForList(String sql, Object... args) {
        QueryRunner queryRunner=new QueryRunner();
        List<Book> bookList = null;
        //获取数据库连接
        Connection connection = ConnectionPool.popConnection();
        //hander对象
        BeanListHandler<Book> handler = new BeanListHandler<Book>(Book.class);
        //执行查询操作
        try {
            bookList = queryRunner.query(connection, sql, handler, args);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionPool.pushConnection(connection);
        }
        return bookList;
    }

    @Override
    public Map<String, Object> queryForMap(String sql, Object... args) {
        QueryRunner queryRunner=new QueryRunner();
        Map<String, Object> map = null;
        //获取数据库连接对象
        Connection connection = ConnectionPool.popConnection();
        //Handler对象
        MapHandler mapHandler = new MapHandler();
        //执行sql语句
        try {
            map = queryRunner.query(connection, sql, mapHandler, args);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionPool.pushConnection(connection);
        }
        return map;
    }

    @Override
    public List<Map<String, Object>> queryForMapList(String sql, Object... args) {
        QueryRunner queryRunner = new QueryRunner();
        List<Map<String, Object>> mapList = null;
        //获取数据库连接Connection对象
        Connection connection = ConnectionPool.popConnection();
        //hander
        MapListHandler mapListHandler = new MapListHandler();
        //执行SQL语句
        try {
            mapList = queryRunner.query(connection, sql, mapListHandler, args);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionPool.pushConnection(connection);
        }
        return mapList;
    }

    @Override
    public Object[] queryForAttributeArray(String sql, Object... args) {
        QueryRunner queryRunner = new QueryRunner();
        Object[] objects = null;
        //获取数据库连接Connection对象
        Connection connection = ConnectionPool.popConnection();
        //handler
        ArrayHandler arrayHandler = new ArrayHandler();
        //执行SQL语句
        try {
            objects = queryRunner.query(connection, sql, arrayHandler, args);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionPool.pushConnection(connection);
        }
        return objects;
    }

    @Override
    public List<Object[]> queryForAttributeArrayList(String sql, Object... args) {
        QueryRunner queryRunner = new QueryRunner();
        List<Object[]> objects =null;
        //获取数据库连接Connection对象
        Connection connection = ConnectionPool.popConnection();
        //hander
        ArrayListHandler arrayListHandler = new ArrayListHandler();
        //执行SQL语句
        try {
            objects = queryRunner.query(connection, sql, arrayListHandler, args);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionPool.pushConnection(connection);
        }
        return objects;
    }

    @Override
    public Object queryForAttributeList(String sql,String colname, Object... args) {
        QueryRunner queryRunner =new QueryRunner();
        Object resCount = null;
        //获取数据库连接Connection对象
        Connection connection = ConnectionPool.popConnection();
        //Handler
        ColumnListHandler columnListHandler = new ColumnListHandler(colname);
        //执行SQL语句
        try {
            resCount = queryRunner.query(connection, sql, columnListHandler, args);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionPool.pushConnection(connection);
        }
        return resCount;
    }

    @Override
    public Object queryForCount(String sql,Object... args) {
        QueryRunner queryRunner =new QueryRunner();
        Object resCount = 0L;
        //获取数据库连接Connection对象
        Connection connection = ConnectionPool.popConnection();
        //Handler
        ScalarHandler scalarHandler = new ScalarHandler();
        //执行SQL语句
        try {
            resCount = queryRunner.query(connection, sql, scalarHandler, args);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionPool.pushConnection(connection);
        }
        return resCount;
    }

    @Override
    public int updateBook(String sql, Object... args) {
        QueryRunner queryRunner =new QueryRunner();
        int rows = 0 ;
        //获取数据库连接Connection对象
        Connection connection = ConnectionPool.popConnection();
        //执行SQL语句
        try {
            rows = queryRunner.update(connection, sql, args);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionPool.pushConnection(connection);
        }
        return rows;
    }


}
